package com.example.flshtarvel.controller;

import com.example.flshtarvel.entity.User;
import com.example.flshtarvel.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    UserService userService;


    @PostMapping(value="/doLogin")
    public Map<String, Object> doLogin(User user, HttpSession session){
        Map<String,Object> map = new HashMap<>();
        User userResult = userService.doLogin(user);

        if(userResult==null){
            //登录失败
            map.put("msg","0");
        }else{
            if(userResult.getRole()==0){//普通用户
                //登陆成功
                session.setAttribute("userId", userResult.getUserId());
                map.put("msg","1");
            }else{
                map.put("msg","0");
            }

        }
        return map;

    }

    @PostMapping(value="/adminLogin")
    public Map<String, Object> adminLogin(User user, HttpSession session){
        Map<String,Object> map = new HashMap<>();
        User userResult = userService.doLogin(user);
        if(userResult==null){
            //登录失败
            map.put("msg","0");
        }else{
            if(userResult.getRole()==1){//管理员
                //登陆成功
                session.setAttribute("userId", userResult.getUserId());
                map.put("msg","1");
            }else{
                map.put("msg","0");
            }
        }
        return map;
    }
}
